package C8_2_ReplaceDataValueWithObject;

import javafx.print.Collation;

import java.util.Collection;

/**
 * Desc:&emsp;&ensp;将值对象改为引用对象
 *
 * @author 李渊&emsp;
 * @version 1.0
 * @since V4.3&emsp;2022/7/13
 */
public class C8_2_demo {

    //==========重构前===========
    // 订单类
    class Order_1{
        // 客户
        private String _cus;

        public Order_1(String _cus) {
            this._cus = _cus;
        }

        public String get_cus() {
            return _cus;
        }

        public void set_cus(String _cus) {
            this._cus = _cus;
        }
    }
    // 使用order类的方法
    public static final int numberOfOrderFor(Collection<Order_1> orders, String cus) {
        return  (int) orders.stream().filter(order -> order.get_cus().equals(cus)).count();
    }

    //==========重构后===========
    // 使用cus类来表示"客户"的概念
    class Cus{
        private final String name;

        public Cus(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    // 订单类
    class Order_2{
        // 客户
        private Cus _cus;

        public Order_2(String cusName) {
            this._cus = new Cus(cusName);
        }

        public String get_cus() {
            return _cus.getName();
        }

        // 之前的String是值对象，不可变，通过在setter中重新new来让cus对象为不可变对象，来避免一些别名问题
        public void set_cus(String cusName) {
            this._cus = new Cus(cusName);
        }
    }

}
